package logic

import (
	"cart_srv/cathe"
	"cart_srv/models"
	"context"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *cart.GoodsAddRequest) (*cart.GoodsAddResponse, error) {
	var g models.Goods
	g.Title = in.Title
	g.Cover = in.Cover
	g.Price = float64(in.Price)
	g.Num = int(in.Num)
	err := g.CreateGoods()
	if err != nil {
		logx.Error("发布商品失败")
		return nil, err
	}

	cathe.SyncGoodsStock(int(g.ID), g.Num)

	return &cart.GoodsAddResponse{
		GoodsId: int64(g.ID),
	}, nil
}
